package cn.vic.genertor.other;

import cn.vic.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: vic
 * @Date: 2018/12/25 8:23
 */
public class MarkDownFormat {

    /**
     * markDown 字符串
     * 通过\n切割
     *
     * @param markDownStr
     * @return
     */
    public String format(String markDownStr) {
        if (markDownStr == null || markDownStr.length() == 0) {
            return null;
        }
        List<String> marks = new ArrayList<>();
        for(String md: markDownStr.split("\n")){
            if(md!=null&& md.length()>0){
                marks.add(md.trim());
            }
        }
        return format(marks);
    }

    /**
     * MarkDown格式化方法
     *
     * @param marks
     * @return
     */
    public String format(List<String> marks) {
        if (marks == null || marks.size() == 0) {
            return null;
        }
        List<String[]> cellsList = new ArrayList<>();
        //每列最大的值
        List<Integer> columnWidthList = new ArrayList<>();
        //取到每个列中最长的值，作为列宽
        for (String m : marks) {
            String[] cells = m.split("[|]");
            expansionListSize(columnWidthList, cells.length);
            for (int i = 1; i < cells.length; i++) {
                //计算列宽
                int widthNum = cellWidthCalc(cells[i]);
                int oldWidthValue = columnWidthList.get(i);
                if (widthNum > oldWidthValue) {
                    columnWidthList.set(i, widthNum);
                }
            }
            cellsList.add(cells);
        }
        //value.trim() 后进行填充空格，以保持列宽。
        StringBuffer markDownBuff = new StringBuffer();
        for (String[] cells : cellsList) {
            StringBuffer cellBuff = new StringBuffer();
            cellBuff.append("|");
            for (int i = 1; i < cells.length; i++) {
                String cellValue = cells[i];
                int widthNum = cellWidthCalc(cellValue);
                int maxWidthValue = columnWidthList.get(i);
                cellBuff.append(cellValue != null ? cellValue.trim() : "").append(widthSpaceOut(maxWidthValue - widthNum + 2)).append("|");
            }
            cellBuff.append("\n");
            markDownBuff.append(cellBuff);
        }
        return markDownBuff.toString();
    }

    /**
     * 空格输出
     *
     * @param widthValue
     * @return
     */
    private String widthSpaceOut(int widthValue) {
        StringBuffer space = new StringBuffer();
        for (int i = 0; i < widthValue; i++) {
            space.append(" ");
        }
        return space.toString();
    }

    /**
     * 根据每行单元格数量扩充最大列
     *
     * @param columnWidthList
     * @param cellsLength
     */
    private void expansionListSize(List<Integer> columnWidthList, int cellsLength) {
        //小于列长度，进行扩容
        if (columnWidthList.size() < cellsLength) {
            int expNum = cellsLength - columnWidthList.size();
            System.out.println(">扩充>"+expNum);
            for (int i = 0; i < expNum; i++) {
                columnWidthList.add(0);
            }
        }
    }

    /**
     * 单元格宽度计算
     * 中文2个空格，英文1个空格
     *
     * @param cellValue
     * @return
     */
    private int cellWidthCalc(String cellValue) {
        double widthNum = 0;
        if (cellValue == null) {
            return 0;
        }
        cellValue = cellValue.trim();
        for (int i = 0; i < cellValue.length(); i++) {
            char c = cellValue.charAt(i);
            if (StringUtils.isChinese(c)) {
                widthNum += 1.72;
            } else {
                widthNum += 1;
            }
        }
        return (int) Math.ceil(widthNum);
    }


}
